package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.UserDiscountMapper;
import com.koron.css2.serviceManage.mapper.UserMixtureMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 工程管理-单条立户信息删除
 * 1. 删除临时用户信息
 * 2. 删除临时混合用水信息
 * 3. 删除临时折扣信息
 * 4. 删除立户信息
 */
public class PjMeterDelete implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjMeterQuery pjMeterQuery = JsonUtils.objectToPojo(req.getData(),PjMeterQuery.class);
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            UserMixtureMapper userMixtureMapper = factory.getMapper(UserMixtureMapper.class);
            UserDiscountMapper userDiscountMapper = factory.getMapper(UserDiscountMapper.class);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);

            // 1. 查出数据
            // 20220224: 兼容删除未入库的数据
            if (StringUtils.isNotEmpty(pjMeterQuery.getId())) {
                PjMeter pjMeter = pjMeterMapper.getSingle(pjMeterQuery);
                Objects.requireNonNull(pjMeter,"参数错误");
                String userInfoTempId = pjMeter.getUserInfoTempId();
                UserBean userBean = userInfoMapper.getUserInfoById(userInfoTempId, UserBean.USERTEMP);
                String userNo = userBean.getUserNo();
                // 2. 删除用户、账户、客户信息
                userInfoMapper.userInfoDelete(userBean.getId(), UserBean.USERTEMP);
                //非主表pj_info_new的客户信息和账户信息才可以删除
                PjInfoBean infoBean = pjInfoMapper.selectById(pjMeter.getConsultId());
                List<PjMeter> listByConsultId = pjMeterMapper.getListByConsultId(pjMeter.getConsultId());
                if (infoBean.getUserAccountTempId().equals(pjMeter.getUserAccountTempId())||infoBean.getUserAccountTempId().equals(pjMeter.getUserAccountTempId())){
                    listByConsultId=listByConsultId.stream().filter(new Predicate<PjMeter>() {
                        @Override
                        public boolean test(PjMeter pjMeter) {
                            return !((pjMeter.getUserAccountTempId().equals(infoBean.getUserAccountTempId()))&&pjMeter.getUserCtmTempId().equals(infoBean.getUserCtmTempId()));
                        }
                    }).collect(Collectors.toList());
                    if (listByConsultId.size()<=0){
                        throw new RuntimeException("禁止删除,最少保存一条数据");
                    }
                    //5.1更新主表
                    infoBean.setUserAccountTempId(listByConsultId.get(0).getUserAccountTempId());
                    infoBean.setUserCtmTempId(listByConsultId.get(0).getUserCtmTempId());
                    pjInfoMapper.updateTempId(infoBean);
                }


                accountInfoMapper.accountInfoDelete(pjMeter.getUserAccountTempId(), AccountInfoBean.ACCOUNTTEMP);
                ctmInfoMapper.ctmInfoDelete(pjMeter.getUserCtmTempId(), CtmInfoBean.CTMTEMP);
                // 3. 删除混合用水信息
                userMixtureMapper.deleteTempByUserNo(userNo);
                // 4. 删除折扣信息
                userDiscountMapper.deleteTempByUserNo(userNo);
                // 5. 删除立户数据
                pjMeterMapper.deleteById(pjMeter.getId());

                // 6. 返回结果
                return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "立户单条数据删除成功", null);
            }
            else {
                return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "立户单条数据删除成功", null);
            }
        } catch (Exception e) {
            logger.error("立户单条数据删除失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"立户单条数据删除失败:"+e.getMessage(),null);
        }
    }
}
